package com.pengbei.enshrine.ui.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.pengbei.enshrine.R;
import com.pengbei.enshrine.base.BaseActivity;
import com.pengbei.enshrine.ui.account.AccountFragment;
import com.pengbei.enshrine.ui.encyclopedia.EncyclopediaFragment;
import com.pengbei.enshrine.ui.home.HomeFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private BottomNavigationView bottomNavigationView;
    private ViewPager2 viewPager;
    private MyFragmentStateAdapter pagerAdapter;
    private List<Fragment> fragmentList;
    private HomeFragment homeFragment;
    private EncyclopediaFragment encyclopediaFragment;
    private AccountFragment accountFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        initFragment();
        initView();
    }

    private void initView() {
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
        viewPager = (ViewPager2) findViewById(R.id.viewPager);

        bottomNavigationView.setBackground(null);
        bottomNavigationView.setItemIconTintList(null);//设置icon颜色

        pagerAdapter = new MyFragmentStateAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        viewPager.setAdapter(pagerAdapter);

        viewPager.setUserInputEnabled(false);

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.one:
                        viewPager.setCurrentItem(0, false);
                        break;
                    case R.id.two:
                        viewPager.setCurrentItem(1, false);
                        break;
                    case R.id.three:
                        viewPager.setCurrentItem(2, false);
                        break;
                }
                return true;
            }
        });

        viewPager.setOffscreenPageLimit(fragmentList.size());

        removeTooltipOnLongPress(bottomNavigationView);//去掉长按显示popupwindow

    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        encyclopediaFragment = new EncyclopediaFragment();
        accountFragment =new AccountFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(homeFragment);
        fragmentList.add(encyclopediaFragment);
        fragmentList.add(accountFragment);
    }

    private static class MyFragmentStateAdapter extends FragmentStateAdapter {
        private List<Fragment> fragmentList;

        public MyFragmentStateAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragmentList) {
            super(fragmentManager, lifecycle);
            this.fragmentList = fragmentList;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }
    }

    private void removeTooltipOnLongPress(@NonNull BottomNavigationView bottomNavigationView) {
        // 获取 BottomNavigationView 的 MenuView
        View menuView = bottomNavigationView.getChildAt(0);
        if (menuView != null && menuView instanceof ViewGroup) {
            ViewGroup menuGroup = (ViewGroup) menuView;
            for (int i = 0; i < menuGroup.getChildCount(); i++) {
                View menuItemView = menuGroup.getChildAt(i);
                if (menuItemView instanceof BottomNavigationItemView) {
                    menuItemView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            // 返回 true 来拦截长按事件
                            return true;
                        }
                    });
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MainRefreshEvent event) {
        if (event != null && event.message != null) {
            Log.i("shuaxin===", "event.message=" + event.message);
            switch (event.message) {
                case MainRefreshType.MainRefreshSelf://刷新自己
                    break;
                case MainRefreshType.MainRefreshPage1://刷新首页
                    if (null != homeFragment) {
                        homeFragment.refreshData();
                    }
                    break;
                case MainRefreshType.MainRefreshPage2://刷新百科
                    if (null != encyclopediaFragment) {
                        encyclopediaFragment.refreshData();
                    }
                    break;
                case MainRefreshType.MainRefreshPage3://刷新我的
                    if (null != accountFragment) {
                        accountFragment.refreshData();
                    }
                    break;
                case MainRefreshType.MainRefreshPageAll://刷新所有
                    if (null != homeFragment) {
                        homeFragment.refreshData();
                    }
                    if (null != encyclopediaFragment) {
                        encyclopediaFragment.refreshData();
                    }
                    if (null != accountFragment) {
                        accountFragment.refreshData();
                    }
                    break;
            }
        }
    }

    public static void goToThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        context.startActivity(intent);
    }
}
